package com.ny.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

@Service
public class EmailService {
//    实现邮箱发送验证码
    @Autowired
    private JavaMailSender javaMailSender;
    @Value("${spring.mail.username}")
    private String fromEmail;
    public void  sendEmail(String email,String code){
        SimpleMailMessage message = new SimpleMailMessage();
//        发件人
        message.setFrom(fromEmail);
//        收件
        message.setTo(email);
//      邮箱的主题
        message.setSubject("邮箱验证码");
        message.setText("您的验证码是："+code+",有效期10分钟，请尽快使用");
        javaMailSender.send(message);
    }
//    生成4位数数字验证码
    public String  generateCode(){
        int code = (int)(Math.random()*9000)+1000;// 1000-9999
        return String.valueOf(code); // int转成String

    }




}
